package com.itheima;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;

//配置指定顺序执行

@WebFilter(value ="/Servlet",dispatcherTypes= {DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE})

public class FilterDemo2 implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        System.out.println("FilterDemo2执行了");
        //获取域中username的数据
        Object username = servletRequest.getAttribute("username");
        //判断域中的数据username是否为admin
        if(username.equals("admin")){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            servletResponse.setContentType("text/html;charset=UTF-8");
            servletResponse.getWriter().write("权限不足");
        }
    }
}
